#!/usr/bin/env python
#coding=utf-8
##########################################################################
# File Name: tsubprocess.py
# Author: wangdj
# mail: wangdajunttf@163.com
# Created Time: Tue 08 Dec 2020 10:30:18 PM CST
##########################################################################


import subprocess
import sys


def run_shell(shell):
    cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True,
                           stdout=sys.stdout, universal_newlines=True, shell=True, bufsize=1)
    cmd.communicate()
    return cmd.returncode

def run_shell1(cmd):
  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
  print(type(p))
  lines = []
  for line in iter(p.stdout.readline, b''):
      line = line.strip().decode("GB2312")
      print(">>>", line)
      lines.append(line)
  return lines

#这是一次性输出
def run_shell2(cmd):
  current_encoding='utf-8'
  popen = subprocess.Popen(cmd,
    shell=True,
    stdout = subprocess.PIPE,
    stderr = subprocess.PIPE,
    bufsize = 1)
  out,err = popen.communicate()
  print('std_out: ' + out.decode('utf-8'))
  #print('std_err: ' + err)
  #print('returncode: ' + str(popen.returncode))

if __name__ == '__main__':
    #print(run_shell("ping www.baidu.com"))
    run_shell2("sh /root/aa.sh")

